// pages/tabBar/buy/buy.js
const app = getApp();
var wxpay = require('../../utils/pay.js')
var memberUtil=require('../../utils/memberUtil');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { num: '1', id: '0', name: '0', value: '消费金支付', img: '../../../../images/bw.png', checked: 'true' },
      { num: '1', id: '1', name: '1', value: '微信支付', img: '../../../../images/bx.png' }
    ],
    shangcheng:null,
    jifen:null,
    userInfo:null,
    totalPrice:null,
    orderId:null,
    orderNumber:null,
    showPay:null,
    beLongTo:null,
    isPay: true,  //支付
    Length: 6,    //输入框个数
    isFocus: false,  //聚焦
    Value: "",    //输入的内容
    ispassword: true, //是否密文显示 true为密文， false为明文。
    pay:null,
    goodsName:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var belongTo=options.belongTo;
    var totalPrice=Number(options.totalPrice);

    that.setData({
      totalPrice:totalPrice,
      beLongTo:belongTo,
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo,
      orderId:options.orderId,
      orderNumber:options.orderNumber,
      goodsName:options.goodsname
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,3);
    }else{
      that.xiaofeijinAPI();
    }


  },
  //调用消费金
  xiaofeijinAPI: function () {
    var that=this;
    var totalPrice=Number(that.data.totalPrice);
    var xiaofeijin=0;
    var jifen=0;
    var payItems=[];
    var showPay=null;
    //调用接口获取信息
    var memberIdJson={memberId:that.data.userInfo.id}
    wx.request({
      url: app.globalData.baseUrl+'/member/findICCCGByMemberId',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(memberIdJson),
      success:function(res){
        if(res.data.status==200){
          xiaofeijin=Number(res.data.data.consumeAmount);
          jifen=Number(res.data.data.integralAmount);
          if(that.data.beLongTo==0){//商城支付
            if(xiaofeijin>=totalPrice){
              payItems.push({checkFlag:true,name:"消费金支付",iconUrl:"/images/icon/icon4.png",payNum:totalPrice,type:'xfj'})
              payItems.push({checkFlag:false,name:"微信支付",iconUrl:"/images/icon/icon5.png",payNum:0,type:'xj'})
            }else if(xiaofeijin<totalPrice && xiaofeijin>0){
              payItems.push({checkFlag:true,name:"消费金支付",iconUrl:"/images/icon/icon4.png",payNum:xiaofeijin,type:'xfj'})
              payItems.push({checkFlag:true,name:"微信支付",iconUrl:"/images/icon/icon6.png",payNum:that.pasrFl(totalPrice,"-",xiaofeijin),type:'xj'})
            }else if(xiaofeijin<=0){
              payItems.push({checkFlag:false,name:"消费金支付",iconUrl:"/images/icon/icon3.png",payNum:0,type:'xfj'})
              payItems.push({checkFlag:true,name:"微信支付",iconUrl:"/images/icon/icon6.png",payNum:totalPrice,type:'xj'})
            }
            showPay={payA:payItems[0].payNum,payB:payItems[1].payNum,nameA:"消费金",nameB:"现金"}
          }else {
            //积分支付
            if (jifen >= totalPrice) {
              payItems.push({checkFlag: true, name: "积分支付", iconUrl: "/images/icon/icon2.png", payNum: totalPrice,type:'jf'})
              payItems.push({checkFlag: false, name: "消费金支付", iconUrl: "/images/icon/icon3.png", payNum: 0,type:'xfj'})
            } else if (jifen < totalPrice && jifen>0) {
              payItems.push({checkFlag: true, name: "积分支付", iconUrl: "/images/icon/icon2.png", payNum: jifen,type:'jf'})
              payItems.push({checkFlag: true, name: "消费金支付", iconUrl: "/images/icon/icon4.png", payNum:that.pasrFl(totalPrice,"-",jifen),type:'xfj'})
            } else if (jifen <= 0) {
              payItems.push({checkFlag:false, name: "积分支付", iconUrl: "/images/icon/icon1.png", payNum: 0,type:'jf'})
              payItems.push({checkFlag: true, name: "消费金支付", iconUrl: "/images/icon/icon4.png", payNum: totalPrice,type:'xfj'})
            }
            showPay = {payA: payItems[0].payNum, payB: payItems[1].payNum, nameA: "积分", nameB: "消费金"}
          }
          that.setData({
            payInfo:payItems,
            shangcheng:{xiaofeijin:xiaofeijin,jifen:jifen},
            showPay:showPay
          })
        }
      }
    })
  },
//支付
pay:function(){
    var that=this;
    var items=that.data.payInfo;
    var weixin=0;
    var xiaofeijin=0;
    var jifen=0;
    var jfyuanMoney=Number(that.data.shangcheng.jifen).toFixed(2);
    var xfjyuanMoney=Number(that.data.shangcheng.xiaofeijin).toFixed(2);
    var payTotalMoney=Number(that.data.totalPrice);
    for(var i=0;i<items.length;i++){
      if(items[i].checkFlag && items[i].type=="xfj"){
        xiaofeijin=Number(items[i].payNum);
      }else if(items[i].checkFlag && items[i].type=="xj"){
        weixin=Number(items[i].payNum);
      }else if(items[i].checkFlag && items[i].type=="jf"){
        jifen=Number(items[i].payNum);
      }
    }
    if(that.data.beLongTo==0) {//商城支付
      if((weixin+xiaofeijin)==payTotalMoney) {
        if((xiaofeijin>0 && xfjyuanMoney==0) && xiaofeijin>xfjyuanMoney) {
          wx.showModal({
            title: '提示',
            content: '您的消费金不够支付，请前往充值',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/my/topUps?totalPrice=' + xiaofeijin
                })
              } else if (res.cancel) {
                return false;
              }
            }
          })
        }else{
          that.payFu("",false);
        }
      }else{
        wx.showModal({
          title: '提示',
          content: '你选择的支付方式付款额与订单支付金额不相等，请重新选择',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
            return false;
          }
        })
      }
    }else{
      //积分支付
      for(var a in items){
        if(items[a].checkFlag){
          if(items[a].type=='jf'){
            if(jfyuanMoney<items[a].payNum){
              wx.showModal({
                title: '提示',
                content: '您的积分不够支付，请前往充值',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.navigateTo({
                      url: '/pages/my/topUps?totalPrice=' + jifen
                    })
                  } else if (res.cancel) {
                    return false;
                  }
                }
              })
              return false;
            }
          }else if(items[a].type=='xfj'){
            if(xfjyuanMoney<items[a].payNum){
              wx.showModal({
                title: '提示',
                content: '您的消费金不够支付，请前往充值',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/my/topUps?totalPrice=' + Number(xiaofeijin-xfjyuanMoney).toFixed(2)
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                    return false;
                  }
                }
              })
              return false;
            }
          }
        }
      }
      //if((xiaofeijin+jifen)==that.data.totalPrice) {
        if ((xiaofeijin+jifen) < that.data.totalPrice) {
          wx.showModal({
            title: '提示',
            content: '您的积分不够支付，请前往充值',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateTo({
                  url: '/pages/my/topUps?totalPrice=' + jifen
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
                return false;
              }
            }
          })
        }else if(((xiaofeijin+jifen)== that.data.totalPrice) && xiaofeijin>0){
          if(xiaofeijin>xfjyuanMoney || jifen>jfyuanMoney){
            wx.showModal({
              title: '提示',
              content: '您的积分不够支付，请前往充值',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateTo({
                    url: '/pages/my/topUps?totalPrice=' + jifen
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                  return false;
                }
              }
            })
          }else{
            that.payFu("",false);
          }

        }else if(((xiaofeijin+jifen)== that.data.totalPrice) && xiaofeijin==0){
          that.payFu("",false);
        }else{
          wx.showModal({
            title: '提示',
            content: '你选择的支付方式付款额与订单支付金额不相等，请重新选择',
            showCancel: false,
            success: function (res) {
              return false;
            }
          })
        }
    }

  },
Focus:function(e) {
  var that = this;
  var inputValue = e.detail.value;
  console.log("输入的内容inputValue="+inputValue);
  that.setData({
    Value: inputValue
  })
  if(inputValue.length==6){
    console.log("密码输入成功password="+inputValue);
    that.payFu(inputValue,true);
  }


},
//支付处理
payFu:function(inputValue,booleanStr){
  var that=this;
  if(booleanStr) {
    //验证密码
    var consumeMoneyPayParamJson = {memberId: that.data.userInfo.id, payPassword: inputValue}
    wx.request({
      url: app.globalData.baseUrl + '/consumemoney/pay/password/validate',
      header: app.globalData.headerPost,
      method: 'POST',
      data: JSON.stringify(consumeMoneyPayParamJson),
      success: function (res) {
        console.log("/consumemoney/pay/password/validate地址调用成功" + JSON.stringify(res.data));
        if (res.data.status == 200) {
          that.wPay();
        } else {
          wx.showToast({
            title: '支付密码错误',
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  }else{
    that.wPay();
  }
},
//调用支付
wPay:function(){
    var that=this;
    //payType 0:两个都有，1：只有消费金（积分）支付  2：只有微信（消费金)
    //调用充值
    var netWeChatPay = 0;
    var wechatPay = 0;
    var xiaofeijin = 0;
    var jifen=0;
    var requstUrl="/orderPay/orderPayment";
    var integralOrderPayParamJson=null;
    var orderPayParamJson=null;
    if (that.data.beLongTo == 0) {//商城支付
      if (that.data.payInfo[0].checkFlag && that.data.payInfo[0].payNum > 0 && that.data.payInfo[1].checkFlag && that.data.payInfo[1].payNum > 0) {
        netWeChatPay = 0;
        wechatPay = that.data.payInfo[1].payNum;
        xiaofeijin = that.data.payInfo[0].payNum;
      } else if (that.data.payInfo[0].checkFlag && that.data.payInfo[0].payNum > 0 && that.data.payInfo[1].payNum == 0) {
        netWeChatPay = 1;
        wechatPay = 0;
        xiaofeijin = that.data.payInfo[0].payNum;
      } else if (that.data.payInfo[1].checkFlag && that.data.payInfo[0].payNum == 0 && that.data.payInfo[1].payNum > 0) {
        netWeChatPay = 2;
        wechatPay = that.data.payInfo[1].payNum;
        xiaofeijin = 0;
      }else{
        wx.showToast({
          title: '请选择正确的支付方式',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      orderPayParamJson={
        memberId: that.data.userInfo.id,
        payType: netWeChatPay,
        totalConsumerMoney: (xiaofeijin==0?"":xiaofeijin),
        openId: that.data.userInfo.openId,
        orderNumber: that.data.orderNumber,
        goodsDescription:that.data.goodsName,
        orderId: that.data.orderId,
        totalWeChatPayMoney: (wechatPay==0?"":wechatPay)
      }
    } else {
      //积分支付
      requstUrl="/integralOrderPay/integralOrderPayment";
      xiaofeijin = that.data.payInfo[1].payNum;
      jifen=that.data.payInfo[0].payNum;
      if (that.data.payInfo[0].checkFlag && that.data.payInfo[0].payNum > 0 && that.data.payInfo[1].checkFlag && that.data.payInfo[1].payNum > 0) {
        netWeChatPay = 0;
      } else if (that.data.payInfo[0].checkFlag && that.data.payInfo[0].payNum > 0 && that.data.payInfo[1].payNum == 0) {
        netWeChatPay = 1;
      } else if (that.data.payInfo[1].checkFlag && that.data.payInfo[0].payNum == 0 && that.data.payInfo[1].payNum > 0) {
        netWeChatPay = 2;
      }
      integralOrderPayParamJson={
        memberId: that.data.userInfo.id,
        payType: netWeChatPay,
        totalConsumerMoney: (xiaofeijin==0?"":xiaofeijin),
        openId: that.data.userInfo.openId,
        orderNumber: that.data.orderNumber,
        goodsDescription: that.data.goodsName,
        orderId: that.data.orderId,
        totalIntegral:(jifen==0?"":jifen)
      }
    }

    wx.request({
      url: app.globalData.baseUrl + requstUrl,
      header: app.globalData.headerPost,
      method: 'POST',
      data: JSON.stringify((that.data.beLongTo == 1?integralOrderPayParamJson:orderPayParamJson)),
      success: function (res) {
        if (res.data.status == 200) {
          if (wechatPay > 0) {
            wxpay.wxpay("/pages/shopcar/money",res.data.data.prepayId,res.data.data.appId,res.data.data.mchApiSecretKey);
          } else {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            });
            wx.redirectTo({
              url: "/pages/shopcar/money"
            });
          }
        }else if(res.data.status == 200){
          if(that.data.beLongTo == 0){//商城支付
            wx.showModal({
              title: '提示',
              content: '您的消费金不足，请去充值',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/my/topUps?totalPrice=' + xiaofeijin
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
                return false;
              }
            })
          }else{
            //积分支付
            wx.showModal({
              title: '提示',
              content: '您的消费金不足，请去充值',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/my/topUps?totalPrice=' + xiaofeijin
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
                return false;
              }
            })
          }

        } else {
          wx.showToast({
            title: '支付失败',
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
Tap:function() {
  var that = this;
  that.setData({
    isFocus: true
  })
},
  //提交
  formSubmit:function(e) {
  this.setData({
    Value: e.detail.value.password
  })

},
//支付
isPayShow:function() {
  this.setData({
    isPay: !(this.data.isPay)
  })
  console.log(this.data.isPay)
},
//减
  reduce:function(e){
    var that=this;
    var type= e.currentTarget.dataset.type;
    var items=that.data.payInfo;
    var shangcheng=that.data.showPay;
    if(type=='xfj'){
      var current=0;
      if(that.data.beLongTo==0){
        //商城支付
        //消费金和现金都需要支付
        if(items[0].payNum>0 && items[1].payNum>0 && items[0].checkFlag){
          if(items[0].payNum>1) {
            current = items[0].payNum = that.pasrFl(items[0].payNum, "-", 1);
            items[1].payNum = that.pasrFl(items[1].payNum, "+", 1);
            if (current == 0) {
              items[0].checkFlag = false;
            }
            items[0].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        }else if(items[0].payNum>0 && items[1].payNum==0 && items[0].checkFlag){//只有消费金支付
          if(items[0].payNum>1) {
            current = items[0].payNum = that.pasrFl(items[0].payNum, "-", 1);
            items[1].payNum = that.pasrFl(items[1].payNum, "+", 1);
            if (current == 0) {
              items[0].checkFlag = false;
            }
            items[0].payNum = current;
            items[1].checkFlag = true;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        }else if(items[0].payNum==0 && items[1].payNum>0 && items[0].checkFlag){//只有现金支付
          items[0].checkFlag=false;
          shangcheng={payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
        }
      }else{
        //积分支付
        //消费金和积分都需要支付
        if(items[0].payNum>0 && items[1].payNum>0 && items[1].checkFlag){
          if(items[1].payNum>1) {
            current = items[1].payNum = that.pasrFl(items[1].payNum, "-", 1);
            items[0].payNum = that.pasrFl(items[0].payNum, "+", 1);
            if (current == 0) {
              items[1].checkFlag = false;
            }
            items[1].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
          }
        }else if(items[0].payNum>0 && items[1].payNum==0 && items[1].checkFlag){//只有积分支付
          items[1].checkFlag=false;
          shangcheng={payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
        }else if(items[0].payNum==0 && items[1].payNum>0 && items[1].checkFlag){//只有消费金支付
          if(items[1].payNum>1) {
            items[0].checkFlag = true;
            current = items[1].payNum = that.pasrFl(items[1].payNum, "-", 1);
            items[0].payNum++;
            if (current == 0) {
              items[1].checkFlag = false;
            }
            items[1].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        }
      }


    }else if(type=='xj'){
      var current=0;
      if(items[1].checkFlag &&  items[1].payNum>0 && items[0].payNum>0){//消费金和现金都支付
        if(items[1].payNum>1) {
          current = items[1].payNum = that.pasrFl(items[1].payNum, "-", 1);
          items[0].payNum = that.pasrFl(items[0].payNum, "+", 1);
          if (current == 0) {
            items[1].checkFlag = false;
          }
          items[1].payNum = current;
          shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
        }
      }else if(items[1].checkFlag &&  items[1].payNum==0 && items[0].payNum>0){//消费金
        items[1].checkFlag=false;
        shangcheng={payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
      }else if(items[1].checkFlag &&  items[1].payNum>0 && items[0].payNum==0){//现金
        if(items[1].payNum>1) {
          current = items[1].payNum = that.pasrFl(items[1].payNum, "-", 1);
          items[0].payNum = that.pasrFl(items[0].payNum, "+", 1);
          items[0].checkFlag = true;
          if (current == 0) {
            items[1].checkFlag = false;
          }
          items[1].payNum = current;
          shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
        }
      }
    }else if(type=='jf'){
      var current=0;
      if(items[0].checkFlag &&  items[0].payNum>0 &&  items[1].payNum>0){//消费金和现金都支付
        if(items[0].payNum>1) {
          current = items[0].payNum = that.pasrFl(items[0].payNum, "-", 1);
          items[1].payNum = that.pasrFl(items[1].payNum, "+", 1);
          if (current == 0) {
            items[0].checkFlag = false;
          }
          items[0].payNum = current;
          shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
        }
      }else if(items[0].checkFlag &&  items[0].payNum>0 &&  items[1].payNum==0){//积分
        if(items[0].payNum>1) {
          current = items[0].payNum = that.pasrFl(items[0].payNum, "-", 1);
          items[1].payNum = that.pasrFl(items[1].payNum, "+", 1);
          items[1].checkFlag = true;
          if (current == 0) {
            items[0].checkFlag = false;
          }
          items[0].payNum = current;
          shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
        }
      }else if(items[0].checkFlag &&  items[0].payNum==0 &&  items[1].payNum>0){//消费金
        items[0].checkFlag=false;
        shangcheng={payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
      }
    }
    that.setData({
      payInfo:items,
      showPay:shangcheng
    })
  },
  //加
  add:function(e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    var items = that.data.payInfo;
    var shangcheng = that.data.showPay;
    if (type == 'xfj') {
      var current = 0;
      if (that.data.beLongTo == 0) {
        //商城支付
        //消费金和现金都需要支付
        if (items[0].payNum > 0 && items[1].payNum > 0 && items[0].checkFlag) {
          if(items[1].payNum>1 && items[0].payNum<that.data.totalPrice) {
            items[0].payNum = that.pasrFl(items[0].payNum, "+", 1);
            current = items[1].payNum = that.pasrFl(items[1].payNum, "-", 1);
            if (current == 0) {
              items[1].checkFlag = false;
            }
            items[1].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        } else if (items[0].payNum > 0 && items[1].payNum == 0 && items[0].checkFlag) {//只有消费金支付
          if (items[0].payNum < that.data.totalPrice) {
            items[0].payNum=that.pasrFl(items[0].payNum,"+",1);
            current = 0;
            if (current == 0) {
              items[1].checkFlag = false;
            }
            items[1].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        } else if (items[0].payNum == 0 && items[1].payNum > 0 && items[0].checkFlag) {//只有现金支付
          if (items[0].payNum < that.data.totalPrice && items[1].payNum>1) {
            items[0].payNum=that.pasrFl(items[0].payNum,"+",1);
            items[0].checkFlag = true;
            current = items[1].payNum =that.pasrFl(items[1].payNum,"-",1);
            if (current == 0) {
              items[1].checkFlag = false;
            }
            items[1].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}

          }
        }
      } else {
        //积分支付
        //消费金和积分都需要支付
        if (items[0].payNum > 0 && items[1].payNum > 0 && items[1].checkFlag) {
          if (items[1].payNum < that.data.totalPrice && items[0].payNum>1) {
            current = items[0].payNum =that.pasrFl(items[0].payNum,"-",1);
            items[1].payNum=that.pasrFl(items[1].payNum,"+",1);
            if (current == 0) {
              items[0].checkFlag = false;
            }
            items[0].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
          }
        } else if (items[0].payNum > 0 && items[1].payNum == 0 && items[1].checkFlag) {//只有积分支付
          if (items[1].payNum < that.data.totalPrice && items[0].payNum>1) {
            current = items[0].payNum =that.pasrFl(items[0].payNum,"-",1);
            items[1].payNum=that.pasrFl(items[1].payNum,"+",1);
            items[1].checkFlag = true;
            if (current == 0) {
              items[0].checkFlag = false;
            }
            items[0].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
          }

        } else if (items[0].payNum == 0 && items[1].payNum > 0 && items[1].checkFlag) {//只有消费金支付
          if (items[1].payNum < that.data.totalPrice && items[0]>1) {
            current = items[0].payNum =that.pasrFl(items[0].payNum,"-",1);
            items[1].payNum=that.pasrFl(items[1].payNum,"+",1);
            if (current == 0) {
              items[0].checkFlag = false;
            }
            items[0].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        }
      }


    } else if (type == 'xj') {
      var current = 0;
      if (that.data.beLongTo == 0) {
        //商城支付
        //消费金和现金都需要支付
        if (items[0].payNum > 0 && items[1].payNum > 0 && items[1].checkFlag) {
          if(items[1].payNum<that.data.totalPrice && items[0].payNum>1) {
            items[1].payNum = that.pasrFl(items[1].payNum, "+", 1);
            current = that.pasrFl(items[0].payNum, "-", 1);
            if (current == 0) {
              items[0].checkFlag = false;
            }
            items[0].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        } else if (items[0].payNum > 0 && items[1].payNum == 0 && items[1].checkFlag) {//只有消费金支付
          if (items[1].payNum < that.data.totalPrice && items[0].payNum>1) {
            items[1].payNum=that.pasrFl(items[1].payNum,"+",1);
            current = items[0].payNum=that.pasrFl(items[0].payNum,"-",1);
            if (current == 0) {
              items[0].checkFlag = false;
            }
            items[0].payNum = current;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        } else if (items[0].payNum == 0 && items[1].payNum > 0 && items[1].checkFlag) {//只有现金支付
          if (items[1].payNum < that.data.totalPrice ) {
            items[1].payNum=that.pasrFl(items[1].payNum,"+",1);
            items[0].checkFlag = false;
            items[0].payNum =0;
            shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "消费金", nameB: "现金"}
          }
        }
      }
    } else if (type == 'jf') {
      var current = 0;
      if (items[0].checkFlag && items[0].payNum > 0 && items[1].payNum > 0) {//消费金和现金都支付
        if (items[0].payNum < that.data.totalPrice && items[1].payNum>1) {
          current = items[1].payNum= that.pasrFl(items[1].payNum,"-",1);
          items[0].payNum= that.pasrFl(items[0].payNum,"+",1);
          if (current == 0) {
            items[1].checkFlag = false;
          }
          items[1].payNum = current;
          shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
        }
      } else if (items[0].checkFlag && items[0].payNum > 0 && items[1].payNum == 0) {//积分
        if (items[0].payNum < that.data.totalPrice) {
          current = 0;
          items[0].payNum=that.pasrFl(items[0].payNum,"+",1);
          if (current == 0) {
            items[1].checkFlag = false;
          }
          items[1].payNum = current;
          shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
        }
      } else if (items[0].checkFlag && items[0].payNum == 0 && items[1].payNum > 0) {//消费金
        if (items[0].payNum < that.data.totalPrice && items[1].payNum>1) {
          items[0].payNum=that.pasrFl(items[0].payNum,"+",1);
          items[0].checkFlag = true;
          current = items[1].payNum=that.pasrFl(items[1].payNum,"-",1);
          if (current == 0) {
            items[1].checkFlag = false;
          }
          items[1].payNum = current;
          shangcheng = {payA: items[0].payNum, payB: items[1].payNum, nameA: "积分", nameB: "消费金"}
        }
      }
    }
    that.setData({
      payInfo:items,
      showPay:shangcheng
    })
  },
//处理函数
pasrFl:function(a,fuhao,b){
  if(fuhao=="+"){
    return parseFloat(Number(a) + Number(b));
  }else if(fuhao=="-"){
    return parseFloat(Number(a) - Number(b)).toFixed(2);
  }

},
//checkbox选择
checkCatch:function(e){
  var that=this;
  var type= e.currentTarget.dataset.type;
  for(var i in that.data.payInfo){
    if(that.data.payInfo[i].type==type){
      that.data.payInfo[i].checkFlag = !(that.data.payInfo[i].checkFlag);
    }
  }
  that.setData({
    payInfo:that.data.payInfo
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})